﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCGPSCommon
{
    /// <summary>
    /// Redis缓存管理服务
    /// </summary>
    public interface IRedisService
    {
        /// <summary>
        /// 是否存在Redis服务器
        /// </summary>
        bool ServerExists { get; set; }

        /// <summary>
        /// 插入新Hash值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashKey"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        Task AddHashAsync<T>(string key, string hashKey, T obj);

        /// <summary>
        /// 获取Hash表中的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashKey"></param>
        /// <returns></returns>
        T GetHashObj<T>(string key, string hashKey);

        /// <summary>
        /// 删除Hash表中的对象
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashKey"></param>
        /// <returns></returns>
        Task<bool> DeleteHashObjAsync(string key, string hashKey);

        /// <summary>
        /// 插入新字符串值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="obj"></param> 
        Task<bool> AddAsync<T>(string key, T obj);

        /// <summary>
        /// key 是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool Exists(string key);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync(string key);

        /// <summary>
        /// 获取指定Key Hash所有值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<HashEntry[]> GetHashAllAsync(string key);

        /// <summary>
        /// 清零数据库
        /// </summary>
        /// <param name="databasesNo"></param>
        void FlushAsync(int databasesNo);

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        T Get<T>(string key) where T:class;


        /// <summary>
        /// 获取分组数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<RedisValue[]> GetGroupList(string groupKey);


        bool Add<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?));
    }
}
